package com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.RecyclerView
import com.facebook.ads.NativeAdLayout
import com.google.android.gms.ads.formats.UnifiedNativeAd
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.NativeAdUtil

/**
 * 原生广告通用承载布局
 * NativeAdLayout -> Facebook承载布局
 * FrameLayout -> AdMob承载布局
 */
class NativeADViewHolder(val mContext: Context, itemView: View) :
    RecyclerView.ViewHolder(itemView) {

    val root = itemView.findViewById<FrameLayout>(R.id.linear_root)
    val adFrame = itemView.findViewById<FrameLayout>(R.id.ad_frame)
    val fbAd = itemView.findViewById<NativeAdLayout>(R.id.native_ad_container)
    val layoutParams: ViewGroup.LayoutParams = root.layoutParams

    // 通过advertiser加载不同类型AdLayout
    fun changeAD(type: String) {
        when (type) {
            ADConfig.FB -> {
                fbAd.visibility = View.VISIBLE
                adFrame.visibility = View.GONE
            }
            ADConfig.ADMOB -> {
                fbAd.visibility = View.GONE
                adFrame.visibility = View.VISIBLE
            }
        }
    }

    // set FacebookAd resource
    fun inflateFacebookAd(nativeAd: com.facebook.ads.NativeAd?, type: String, placement: String) {
        NativeAdUtil.inflateFacebookAd(nativeAd, itemView, type, placement)
    }

    // set AdMobAd resource
    fun inflateAdMobAd(nativeAd: UnifiedNativeAd, type: String) {
        NativeAdUtil.inflateAdMobAd(nativeAd, itemView, type)
    }

    fun inVisibility() {
        //隐藏item要把高度宽度设为0；
        root.visibility = View.GONE
        layoutParams.height = 0
        layoutParams.width = 0
        root.layoutParams = layoutParams
    }

    fun visibility() {
        //显示item要重新设置高度宽度；
        root.visibility = View.VISIBLE
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
        root.layoutParams = layoutParams
    }

}